//
//  HCLogInView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/22.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCLogInView.h"

@interface HCLogInView () {
    UITextField   *userField;
    UITextField *pwdField;
}

@end

@implementation HCLogInView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubView:frame];
    }
    return self;
}

-(void)initializeSubView:(CGRect)frame {
    
    self.backgroundColor = [UIColor blackColor];
    
    UIImageView *bg = [[UIImageView alloc] initWithFrame:frame];
    bg.userInteractionEnabled = YES;
    bg.image = [UIImage imageNamed:@"happy_LogIn_bg"];
    [self addSubview:bg];
    
    UIButton *btn_down = [[UIButton alloc] initWithFrame:CGRectMake(20, 30, 24, 24)];
    [btn_down setBackgroundImage:[UIImage imageNamed:@"下箭头"] forState:UIControlStateNormal];
    [btn_down addTarget:self action:@selector(cancleBtn:) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:btn_down];
    
    //logo1
    UIImageView *im1 = [[UIImageView alloc] initWithFrame:CGRectMake(YBKAdaptiveW(35), YBKAdaptiveH(300), YBKAdaptiveW(19), YBKAdaptiveW(19))];
    im1.image = [UIImage imageNamed:@"happy_login_user"];
    [self addSubview:im1];
    
    //输入
    userField = [[UITextField alloc] initWithFrame:CGRectMake(im1.frame.origin.x+im1.frame.size.width+YBKAdaptiveW(30), im1.center.y-YBKAdaptiveW(8), frame.size.width - YBKAdaptiveW(20) - im1.frame.size.width, YBKAdaptiveW(30))];
    userField.placeholder = @"输入手机号";
    userField.textColor = [UIColor whiteColor];
    [userField setValue:[UIFont boldSystemFontOfSize:10] forKeyPath:@"_placeholderLabel.font"];
    [userField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self addSubview:userField];
    
    //line1
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(YBKAdaptiveW(19), im1.frame.origin.y+im1.frame.size.height+YBKAdaptiveH(15), (frame.size.width - 2.0*YBKAdaptiveW(19)), 0.5)];
    
    line1.backgroundColor = YBKRGBCOLOR(240, 96, 96, 1);
    [self addSubview:line1];
    
    //logo2
    UIImageView *im2 = [[UIImageView alloc] initWithFrame:CGRectMake(im1.frame.origin.x, line1.frame.origin.y+line1.frame.size.height+YBKAdaptiveH(25), YBKAdaptiveW(19), YBKAdaptiveW(24))];
    im2.image = [UIImage imageNamed:@"happy_login_pwd"];
    [self addSubview:im2];
    
    pwdField = [[UITextField alloc] initWithFrame:CGRectMake(im1.frame.origin.x+im1.frame.size.width+YBKAdaptiveW(30), im2.center.y-YBKAdaptiveW(8), frame.size.width - YBKAdaptiveW(20) - im1.frame.size.width, YBKAdaptiveW(30))];
    pwdField.placeholder = @"输入密码";
    pwdField.secureTextEntry = YES;
    pwdField.textColor = [UIColor whiteColor];
    [pwdField setValue:[UIFont boldSystemFontOfSize:10] forKeyPath:@"_placeholderLabel.font"];
    [pwdField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self addSubview:pwdField];
    
    //line2
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(line1.frame.origin.x, im2.frame.origin.y+im2.frame.size.height+YBKAdaptiveH(10), line1.frame.size.width, line1.frame.size.height)];
    line2.backgroundColor = YBKRGBCOLOR(240, 96, 96, 1);
    [self addSubview:line2];
    
    //登录
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(YBKAdaptiveW(20), YBKAdaptiveH(43)+line2.frame.origin.y+line2.frame.size.height, frame.size.width - 2.0*YBKAdaptiveW(20), YBKAdaptiveW(45))];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"happy_login_login"] forState:UIControlStateNormal];
    btn1.tag = 1;
    [self addSubview:btn1];
    
    //注册
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(btn1.frame.origin.x, btn1.frame.size.height+btn1.frame.origin.y+YBKAdaptiveH(25), btn1.frame.size.width, btn1.frame.size.height)];
    btn2.tag = 2;
    [btn2 setBackgroundImage:[UIImage imageNamed:@"happy_login_log"] forState:UIControlStateNormal];
    [self addSubview:btn2];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, btn2.frame.origin.y+btn2.frame.size.height, frame.size.width, frame.size.height - btn2.frame.origin.y- btn2.frame.size.height)];
    view.backgroundColor = [UIColor clearColor];
    [self addSubview:view];
    
    //忘记密码
    UIButton *bvtn = [[UIButton alloc] initWithFrame:CGRectMake((frame.size.width - YBKAdaptiveW(45))/2.0, (view.frame.size.height - YBKAdaptiveW(11))/2.0, YBKAdaptiveW(45), YBKAdaptiveW(11))];
    [bvtn setBackgroundImage:[UIImage imageNamed:@"happy_login_gorget"] forState:UIControlStateNormal];
    bvtn.tag = 3;
    [view addSubview:bvtn];
    
    [btn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bvtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)cancleBtn:(UIButton *)btn {
    [self.delegate logInViewCancle];
}


-(void)btnAction:(UIButton *)btn {
    if (btn.tag == 1) {
        //登录
        [self.delegate logInViewLogInClicked:userField.text password:pwdField.text];
    }else if (btn.tag == 2) {
        //注册
        [self.delegate logInViewLogClicked];
    }else{
        //忘记密码
        [self.delegate logInViewForgetPwd];
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
